﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using MQtoUnity;
public static class NewVoxelSlabRead {

    public static VoxelData GetVoxelData(string Path)
    {
         byte[] _data = File.ReadAllBytes(Path);
         using (MemoryStream _ms = new MemoryStream(_data))
         { 
             using (BinaryReader br = new BinaryReader(_ms))
             {
                 VoxelData newVoxelData = new VoxelData();
                 int SizeX, SizeY, SizeZ;
                 SizeX = br.ReadInt32();
                 SizeZ = br.ReadInt32();
                 SizeY = br.ReadInt32();
                 int MaxSize = SizeX * SizeY * SizeZ;
                 int[, ,] DataId = new int[SizeX, SizeY, SizeZ];
                 for (int x = SizeX - 1; x >= 0; x--)
                 {
                     for (int z = SizeZ - 1; z >= 0; z--)
                     {
                         for (int y = SizeY - 1; y >= 0; y--)
                     {
                       
                             int data = br.ReadByte();
                             //if (data == 255)
                             //{
                             //    data = 0;
                             //}
                             //else
                             //{
                             //    data += 1;
                             //}
                             DataId[x, y, z] = data;
                         }
                     }
                 }
                 Color[] Colors = new Color[256];
                 for (int i = 0; i < 256; i++)
                 {
                     int r = br.ReadByte();
                     int g = br.ReadByte();
                     int b = br.ReadByte();
                     float R = (r * 4);
                     float G = (g * 4);
                     float B = (b * 4);
                     Color color = new Color(R / 255f, G / 255f, B / 255f, 1);
                     Colors[i] = color;
                 }

                 newVoxelData.SizeX = SizeX;
                 newVoxelData.SizeY = SizeY;
                 newVoxelData.SizeZ = SizeZ;
                 newVoxelData.Version = new byte[] { 150, 0, 0, 0 };
                 newVoxelData.Palatte = Colors;
                 newVoxelData.Voxels = DataId;

                 SetVoxelData(newVoxelData, @"C:\Users\Administrator\Desktop\new1.vox");
                 return newVoxelData;
             }
         }
	}

    public static void SetVoxelData(VoxelData newVoxelData, string Path)
    {
        using (FileStream fs = new FileStream(Path, FileMode.Create))
        {
            using (System.IO.BinaryWriter br = new BinaryWriter(fs))
            {

                int SizeX = newVoxelData.SizeX;
                int SizeY = newVoxelData.SizeY;
                int SizeZ = newVoxelData.SizeZ;

                br.Write(newVoxelData.SizeX);
                br.Write(newVoxelData.SizeY);
                br.Write(newVoxelData.SizeZ);

                //br.Write(new byte[] { 150, 0, 0, 0 });
                int[, ,] DataId = newVoxelData.Voxels;
                for (int x = 0; x < SizeX; x++)
                {
                    for (int y = SizeY - 1; y >= 0; y--)
                    {
                        for (int z = SizeZ - 1; z >= 0; z--)
                        {
                            //if (DataId[x, y, z] != 255)
                            //{

                            //}
                            br.Write((byte)DataId[x, y, z]);
                        }
                    }
                }



                Color[] Colors = newVoxelData.Palatte;
                for (int i = 0; i < 256; i++)
                {
                    br.Write((byte)((int)(Colors[i].r *255 / 4)) );
                    br.Write((byte)((int)(Colors[i].g * 255 / 4)));
                    br.Write((byte)((int)(Colors[i].b * 255 / 4)));
                }
            }
        }
    }
	
}
